#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#define LEN_NAME    128



#if 0
max = 8;
1                   -> _______1
12345678            -> 12345678
123456789           -> ...56789
#endif


struct uconststring
{
    const char* s;
    size_t size;
};

const char* kcstr_blank     =   "                                                                                                                                ";
const char* kcstr_hyphens   =   "--------------------------------------------------------------------------------------------------------------------------------";
const char* kcstr_dot       =   "................................................................................................................................";
const char* kcstr_underline =   "________________________________________________________________________________________________________________________________";

int ulog_string_add_prefix(const char* src, size_t max, struct uconststring strs[2])
{
    int retn = 0;

    if(NULL == src || max<3) {
        return 0;
    }

    size_t size_src = strlen(src);
    if(size_src < max) {
        strs[0].s = kcstr_blank;
        strs[0].size = max-size_src;
        strs[1].s = src;
        strs[1].size = size_src;
        retn = 2;
    }
    else if(size_src == max){
        strs[0].s = src;
        strs[0].size = size_src;
        retn = 1;
    }
    else {
        strs[0].s = kcstr_dot;
        strs[0].size = 3;
        strs[1].s = src+(size_src)-(max-3);
        strs[1].size = max-3;
        retn = 2;
    }
    
    return retn;
}


struct ustring
{
    char* s;
    size_t size_total;
    size_t len;
};


int ulog_conststrings_copy(struct ustring* dest, struct uconststring* strs, int n)
{
    int ret = 0;

    int i;
    for(i=0; i<n; i++) {
        if(dest->size_total - dest->len <= strs[i].size) {
            break;
        }

        strncpy(dest->s+dest->len, strs[i].s, strs[i].size);
        dest->len += strs[i].size;
    }
    dest->s[dest->len] = '\0';

    return ret;
}


int main()
{
    const char* s[] = {"1", "12345678", "123456789"};
    char p[LEN_NAME];
    int i;
    int n;
    struct uconststring strs[2];
    struct ustring ps = {
        .s = p,
        .size_total = LEN_NAME,
        .len = 0,
    };

    for(i=0; i<3; i++) {
        n = ulog_string_add_prefix(s[i], 8, strs);
        ps.s[0] = '\0';
        ps.len = 0;
        ulog_conststrings_copy(&ps, strs, n);

        printf("[%s]\n", p);
    }

    return 0;
}





